<template>
   <div class="app-container">
      <a-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" layout="inline">
         <a-form-item label="岗位编码" field="postCode">
            <a-input
               v-model="queryParams.postCode"
               placeholder="请输入岗位编码"
               clearable
               @keyup.enter="handleQuery"
            />
         </a-form-item>
         <a-form-item label="岗位名称" field="postName">
            <a-input
               v-model="queryParams.postName"
               placeholder="请输入岗位名称"
               clearable
               @keyup.enter="handleQuery"
            />
         </a-form-item>
         <a-form-item label="状态" field="status">
            <a-select v-model="queryParams.status" placeholder="岗位状态" style="width: 200px" clearable>
               <a-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </a-select>
         </a-form-item>
         <a-form-item>
            <a-button type="primary" class="button-margin-right" @click="handleQuery">
              <template #icon>
                <icon-Search/>
              </template>
              搜索
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
         </a-form-item>
      </a-form>

      <a-row :gutter="10" class="mb8">
         <a-col :span="1.5">
            <a-button
               type="primary"
               plain
               @click="handleAdd"
               v-hasPermi="['system:post:add']"
            >
              <template #icon>
                <icon-plus/>
              </template>
              新增
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
               status="success"
               type="primary"
               plain
               :disabled="single"
               @click="handleUpdate"
               v-hasPermi="['system:post:edit']"
            >
              <template #icon>
                <icon-edit/>
              </template>
              修改
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
               status="danger"
               type="primary"
               plain
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['system:post:remove']"
            >
              <template #icon>
                <icon-delete/>
              </template>
              删除
            </a-button>
         </a-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </a-row>

      <a-table v-loading="loading" :data="postList"
               row-key="postId" :row-selection="rowSelection"
               v-model:selectedKeys="selectedIds" :pagination="false"
               @selection-change="handleSelectionChange">
        <template #columns>
          <a-table-column title="岗位编号" align="center" data-index="postId" />
          <a-table-column title="岗位编码" align="center" data-index="postCode" />
          <a-table-column title="岗位名称" align="center" data-index="postName" />
          <a-table-column title="岗位排序" align="center" data-index="postSort" />
          <a-table-column title="状态" align="center" data-index="status">
            <template #cell="{record}">
              <dict-tag :options="sys_normal_disable" :value="record.status" />
            </template>
          </a-table-column>
          <a-table-column title="创建时间" align="center" data-index="createTime" width="180">
            <template #cell="{record}">
              <span>{{ parseTime(record.createTime) }}</span>
            </template>
          </a-table-column>
          <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
            <template #cell="{record}">
              <a-button
                  type="text"
                  @click="handleUpdate(record)"
                  v-hasPermi="['system:post:edit']"
              >
                <icon-edit/>
                修改
              </a-button>
              <a-button
                  type="text"
                  @click="handleDelete(record)"
                  v-hasPermi="['system:post:remove']"
              >
                <icon-delete/>
                删除
              </a-button>
            </template>
          </a-table-column>
        </template>

      </a-table>

     <!--分页-->
     <div style="margin-top: 30px;float: right">
       <a-pagination :total="total" show-total show-jumper show-page-size
                     v-model:current="queryParams.pageNum"
                     v-model:page-size="queryParams.pageSize"
                     @change="onPageChange"
                     @page-size-change="onPageSizeChange"
       />
     </div>

      <!-- 添加或修改岗位对话框 -->
     <a-drawer :visible="postDrawerVisible" @ok="handlePostOk" @cancel="handlePostCancel" unmountOnClose :width="540">
       <template #title>
         <h4>{{title}}</h4>
       </template>
       <a-form ref="postRef" :model="form" :rules="rules" label-width="80px">
         <a-form-item label="岗位名称" field="postName">
           <a-input v-model="form.postName" placeholder="请输入岗位名称" />
         </a-form-item>
         <a-form-item label="岗位编码" field="postCode">
           <a-input v-model="form.postCode" placeholder="请输入编码名称" />
         </a-form-item>
         <a-form-item label="岗位顺序" field="postSort">
           <a-input v-model="form.postSort" />
         </a-form-item>
         <a-form-item label="岗位状态" field="status">
           <a-radio-group v-model="form.status">
             <a-radio
                 v-for="dict in sys_normal_disable"
                 :value="dict.value"
                 :label="dict.value"
             >{{ dict.label }}</a-radio>
           </a-radio-group>
         </a-form-item>
         <a-form-item label="备注" field="remark">
           <a-textarea v-model="form.remark" placeholder="请输入内容" />
         </a-form-item>
       </a-form>
     </a-drawer>
   </div>
</template>

<script setup name="Post">
import { listPost, addPost, delPost, getPost, updatePost } from "@/api/system/post";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const postDrawerVisible = ref(false)
const direction = ref('rtl')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
    postName: undefined,
    status: undefined
  },
  rules: {
    postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

const handlePostOk = () => {
  submitForm()
};
const handlePostCancel = () => {
  postDrawerVisible.value = false;
}

const onPageChange = (current) => {
  queryParams.value.pageNum = current
  getList()
};
const onPageSizeChange = (pageSize) => {
  queryParams.value.pageSize = pageSize
  getList()
};

/** 开启表格多选  */
const selectedIds = ref([]);
const rowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
  onlyCurrent: false,
});
// 修改按钮生效
const single = computed(() => {
  return selectedIds.value.length !== 1
});
// 删除按钮生效
const multiple = computed(() => {
  return !selectedIds.value.length
});

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listPost(queryParams.value).then(response => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  postDrawerVisible.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  postDrawerVisible.value = true;
  title.value = "添加岗位";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || ids.value;
  getPost(postId).then(response => {
    form.value = response.data;
    postDrawerVisible.value = true;
    title.value = "修改岗位";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (!valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then(response => {
          proxy.$message.success("修改成功");
          postDrawerVisible.value = false;
          getList();
        });
      } else {
        addPost(form.value).then(response => {
          proxy.$message.success("新增成功");
          postDrawerVisible.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
    return delPost(postIds);
  }).then(() => {
    getList();
    proxy.$message.success("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/post/export", {
    ...queryParams.value
  }, `post_${new Date().getTime()}.xlsx`);
}

getList();
</script>
